import grpc
from mxshop_src.user_srv.proto import user_pb2, user_pb2_grpc
from mxshop_src.user_srv.models import model
from loguru import logger
from passlib.hash import pbkdf2_sha256
import time

class UserServicer(user_pb2_grpc.UserServicer):

    def convert_user_to_rsp(self, user):
        # 将user的model对象转换成message对象
        user_info_rsp = user_pb2.UserInfoResponse()

        user_info_rsp.id = user.id
        user_info_rsp.passWord = user.password
        user_info_rsp.mobile = user.mobile
        user_info_rsp.role = user.role

        if user.nickName:
            user_info_rsp.nickName = user.nickName
        if user.gender:
            user_info_rsp.gender = user.gender
        if user.birthday:
            user_info_rsp.birthDay = int(time.mktime(user.birthday.timetuple()))

        return user_info_rsp

    @logger.catch
    def GetUserByMobile(self, request: user_pb2.MobileRequest, context):
        print("查询的手机是" + request.mobile)

        user_info_rsp = user_pb2.UserInfoResponse()

        user_info_rsp.id = 1
        user_info_rsp.passWord = "admin123"
        user_info_rsp.mobile = "18297651350"

        return user_info_rsp

        # 通过mobile查询用户
        try:
            user = model.User.get(model.User.mobile == request.mobile)
            return self.convert_user_to_rsp(user)
        except:
            context.set_code(grpc.StatusCode.NOT_FOUND)
            context.set_details("用户不存在")
            return user_pb2.UserInfoResponse()
    
    @logger.catch()
    def CheckPassWord(self, request, context):
        return user_pb2.CheckResponse(success=1)
        # return user_pb2.CheckResponse(success=pbkdf2_sha256.verify(request.password, request.encryptedPassword))

    @logger.catch()
    def GetUserList(self, request, context):
        print("haha")
        rsp = user_pb2.UserListResonse()

        users = model.User.select()
        rsp.total = users.count()
        print("用户列表")
        start = 0
        per_page_nums = 10
        if request.pSize:
            per_page_nums = request.pSize
        if request.pn:
            start = per_page_nums * (request.pn - 1)

        users = users.limit(per_page_nums).offset(start)

        for user in users:
            rsp.data.append(self.convert_user_to_rsp(user))

        return rsp

    @logger.catch
    def CreateUser(self, request: user_pb2.CreateUserInfo, context):
        # 新建用户, web开发 - 表单验证 没有必要
        try:
            model.User.get(model.User.mobile == request.mobile)

            context.set_code(grpc.StatusCode.ALREADY_EXISTS)
            context.set_details("用户已存在")
            return user_pb2.UserInfoResponse()
        except:
            pass

        user = model.User()
        user.nick_name = request.nickName
        user.mobile = request.mobile
        user.password = pbkdf2_sha256.hash(request.passWord)
        user.save()

        return self.convert_user_to_rsp(user)

    @logger.catch
    def UpdateUser(self, request: user_pb2.UpdateUserInfo, context):
        return user_pb2.UserInfoResponse()
        # 更新用户
        # try:
        #     user = User.get(User.id == request.id)
        #
        #     user.nick_name = request.nickName
        #     user.gender = request.gender
        #     user.birthday = date.fromtimestamp(request.birthDay)
        #     user.save()
        #     return empty_pb2.Empty()
        # except DoesNotExist:
        #     context.set_code(grpc.StatusCode.NOT_FOUND)
        #     context.set_details("用户不存在")
        #     return user_pb2.UserInfoResponse()
